<template>
  <a-modal title="新建医生" :width="660" :visible="visible" :confirmLoading="loading" @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }">
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">

        <a-form-item label="营业执照" :labelCol="labelCol" :wrapperCol="wrapperCol" size="large">
          <a-upload v-model:file-list="fileList" name="avatar" list-type="picture-card" class="avatar-uploader"
            :show-upload-list="false" action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
            :before-upload="beforeUpload" @change="handleChange">
            <img v-if="imageUrl" :src="imageUrl" alt="avatar" />
            <div v-else>
              <loading-outlined v-if="loading"></loading-outlined>
              <plus-outlined v-else></plus-outlined>
              <div class="ant-upload-text">选择图片</div>
            </div>
          </a-upload>
        </a-form-item>
        <a-form-item label="企业名称" :labelCol="labelCol" :wrapperCol="wrapperCol" size="large">
          <a-input v-decorator="['name', { rules: [{ required: true, min: 5, message: '请输医生名称' }] }]" />
        </a-form-item>
        <a-form-item label="证件信息"  :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['idno', { rules: [{ required: true, min: 18, message: '证件信息' }] }]"  size="large"/>
        </a-form-item>
        <a-form-item label="联系方式"  :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['phone', { rules: [{ required: true, min: 5, message: '联系方式' }] }]"  size="large"/>
        </a-form-item>
        <a-form-item label="地址"  :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['name', { rules: [{ required: true, min: 5, message: '紧急联系人' }] }]"  size="large"/>
        </a-form-item>
        <a-form-item label="备注"  :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea placeholder="" :rows="4" :maxlength="255"  size="large"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'

// 表单字段
const fields = ['description', 'pid']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    // loading: {
    //   type: Boolean,
    //   default: () => false
    // },
    model: {
      type: Object,
      default: () => null
    }
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      labelCol: {lg: { span: 5 },sm: { span: 5 }},
      wrapperCol: {xs: { span: 19 },sm: { span: 19 }},
      form: this.$form.createForm(this),
      //  科室类型列表
      typeList: [
        { id: 1, name: '内科' },
        { id: 2, name: '外科' },
        { id: 3, name: '儿科' },
        { id: 4, name: '妇产科' },
        { id: 5, name: '其他医生' },
      ],
      //  职务列表
      jobList: [
        { id: 1, name: '主任' },
        { id: 2, name: '副主任' },
        { id: 3, name: '住院医师' },
      ],
      fileList: [],
      loading: false,
      imageUrl: null,
    }
  },
  created() {
    console.log('custom modal created')

    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    handleChange() {

    },
    beforeUpload: file => {
      const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
      if (!isJpgOrPng) {
        message.error('You can only upload JPG file!');
      }
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        message.error('Image must smaller than 2MB!');
      }
      return isJpgOrPng && isLt2M;
    },
    uploadchangfge: info => {
      if (info.file.status === 'uploading') {
        this.loading = true;
        return;
      }
      if (info.file.status === 'done') {
        // Get this url from response in real world.
        getBase64(info.file.originFileObj, base64Url => {
          this.imageUrl = base64Url;
          this.loading = false;
        });
      }
      if (info.file.status === 'error') {
        this.loading = false;
        message.error('upload error');
      }
    }
  }
}
</script>
